package com.ghck.server.controller;

import java.security.Principal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ghck.server.controller.access.ArgIndexForPrintLog;
import com.ghck.server.exception.GhckException;
import com.ghck.server.exception.ErrorConstant;
import com.ghck.server.model.Friends;
import com.ghck.server.model.query.FriendsQuery;
import com.ghck.server.model.request.FriendsReq;
import com.ghck.server.model.response.ResultList;
import com.ghck.server.model.response.ResultObject;
import com.ghck.server.security.AjkUserDetails;
import com.ghck.server.service.FriendsService;
import com.ghck.server.utils.AjkUtil;

@RequestMapping("/friend")
@RestController
public class FriendController {

	@Autowired
	FriendsService friendsService;

	/**
	 * 亲友类型
	 * 
	 * @ArgIndexForPrintLog(indexes={0}) @RequestMapping(value =
	 *                                   "/category/all", method =
	 *                                   RequestMethod.GET) public ResultObject
	 *                                   <EaseUser> allCategory(Principal
	 *                                   principal){ Integer userId =
	 *                                   Integer.parseInt(principal.getName());
	 *                                   return null; }
	 */

	/**
	 * 添加亲属
	 */
	@ArgIndexForPrintLog(indexes = { 0 })
	@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = "multipart/form-data")
	public ResultObject<Integer> findMyEasemodAccount(Long birthdayTs, FriendsReq friend, Principal principal) {
		AjkUserDetails user = AjkUtil.getUserDetails(principal);
		if (null == user) {
			throw new GhckException(ErrorConstant.ACCOUNT_NOT_LOGIN.getErrorCode(), "未登录,请先登录");
		}
		Integer userId = Integer.parseInt(principal.getName());
		friend.dealWith();
		friend.setUserId(userId);
		if (birthdayTs != null) {
			friend.setBirthday(new Date(birthdayTs));
		}
		Integer id = friendsService.insertFriends(friend);

		return new ResultObject<>(id);
	}

	/**
	 * 亲属列表
	 */
	@ArgIndexForPrintLog(indexes = { 0 })
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ResultList<Friends> findMyFriends(Integer startId, Integer endId, Integer start, Integer limit,
			Principal principal) {
		AjkUserDetails user = AjkUtil.getUserDetails(principal);
		if(null == user){
			throw new GhckException(ErrorConstant.ACCOUNT_NOT_LOGIN.getErrorCode(), "未登录,请先登录");
		}
		Integer userId = Integer.parseInt(principal.getName());
		FriendsQuery query = new FriendsQuery();
		query.setUserId(userId);
		query.setLimit(0);
		List<Friends> friednds = friendsService.findFriendsByQuery(query);
		return new ResultList<>(friednds);
	}

	/**
	 * 亲友属性
	 */

	@ArgIndexForPrintLog(indexes = { 0 })
	@RequestMapping(value = "/{friendId}/profile", method = RequestMethod.GET)
	public ResultObject<Friends> friendProfile(@PathVariable("friendId") Integer friendId, Principal principal) {
		AjkUserDetails user = AjkUtil.getUserDetails(principal);
		if(null == user){
			throw new GhckException(ErrorConstant.ACCOUNT_NOT_LOGIN.getErrorCode(), "未登录,请先登录");
		}
		Integer userId = Integer.parseInt(principal.getName());
		Friends friednds = friendsService.findFriendsById(friendId);
		return new ResultObject<>(friednds);
	}

	/**
	 * 添加亲属-亲属关系列表
	 * 
	 * @ArgIndexForPrintLog(indexes={0}) @RequestMapping(value = "/", method =
	 *                                   RequestMethod.GET) public ResultObject
	 *                                   <EaseUser> findMyEasemodAccount(
	 *                                   Principal principal){ Integer userId =
	 *                                   Integer.parseInt(principal.getName());
	 *                                   return null; }
	 */
}